#ifndef QBDDMANAGER_H
#define QBDDMANAGER_H


#include "bddmanager_global.h"
#include "qlineanalyzer.h"
#include "db_cxx.h"
#include "qbddfile.h"


#include <QString>
#include <QDateTime>
#include <QFileInfo>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QHash>
class BDDMANAGER_EXPORT QBDDManager
{
public:
   ~QBDDManager();
   static QBDDManager* instance(QString dbFile);
   static QBDDManager* instance() { return m_instance; }
   static void destroy();
   static bool reinit(QString dbFile);

   QList<QResistorItem*> searchR(const QList<QStringList>& terms,const quint8& maxTerms);   
   QList<QCapacitorItem*> searchC(const QList<QStringList>& terms,const quint8& maxTerms);   
   QList<QSelfItem*> searchL(const QList<QStringList>& terms,const quint8& maxTerms);   
   QList<QLedItem*> searchLed(const QList<QStringList>& terms,const quint8& maxTerms);   
   QList<QBDDItem*> searchAll(const QStringList& terms,const quint8& maxTerms);  
   QList<QBDDItem*> searchOther(const QList<QStringList>& terms,const quint8& maxTerms);    

private:
   void createBdd(QString dbFile);
   void destroyBdd();
   void openBdd();
   void closeBdd();
   void listPlp();
   static bool isValid() { return m_valid; };
   void report(QString report);
   void report(QStringList report);

   void addTo(const QString& add,QStringList& list) { if(!list.contains(add))list << add; }

   template <class T>
   QList<T*> searchSpecific(const QList<QStringList>& terms,const quint8& maxTerms);   

   QBDDManager(QString dbFile);
   QHash<QString,Db*> m_bdd;
   static QBDDManager* m_instance;
   static bool m_valid;
   quint32 m_identifier;
};

#define SEARCH_DB_R(__TERM__,__MAX__)   __bdd->searchR(__TERM__,__MAX__)
#define SEARCH_DB_C(__TERM__,__MAX__)   __bdd->searchC(__TERM__,__MAX__)
#define SEARCH_DB_L(__TERM__,__MAX__)   __bdd->searchL(__TERM__,__MAX__)
#define SEARCH_DB_LED(__TERM__,__MAX__)   __bdd->searchLed(__TERM__,__MAX__)
#define SEARCH_DB_OTHER(__TERM__,__MAX__) __bdd->searchOther(__TERM__,__MAX__)
#define SEARCH_DB_ALL(__TERM__,__MAX__) __bdd->searchAll(__TERM__,__MAX__)

#define INIT_DB( __FILE__) QBDDManager::reinit(__FILE__)
#define __bdd   QBDDManager::instance()
#define __kill_bdd QBDDManager::destroy()
#define DATABASE_DIRECTORY      "bdd"
#define DATABASE_REPORT         "bdd_report.log"
#define DATABASE_EXTENSION      ".bdd"

#endif // QBDDMANAGER_H
